//logs.js
const util = require('../../utils/util.js')
var api = require('../../config/api.js');
const { $Toast } = require('../../dist/base/index');
Page({
  data: {
    list: [],
    authVisible: false,
    loginVisible: false,
    reportCountVisible: false,
    reportBackVisible: false,
    currentTime: 0
  },
  onLoad: function () {
    this.setData({
      nbTitle: '新标题',
      nbLoading: true,
      nbFrontColor: '#ffffff',
      nbBackgroundColor: '#000000',
    })
  },
  onShow: function () {
    let self = this;
    wx.getSetting({
      withSubscriptions: true,
      success(res) {

        self.setData({
          authVisible: !res.authSetting['scope.subscribeMessage'],
        })
      }
    })
    util.request(api.BuildList, {
    }, 'POST').then(res => {
      console.log(wx.getStorageSync('userInfo'));
      self.setData({
        list: res,
        loginVisible: wx.getStorageSync('userInfo') == null
      })
    }).catch(err => {
      console.log(err)
    });
    //占座统计
    util.request(api.IsReport, {
    }, 'POST').then(res => {
      if (res.data.count > 3) { //静止预约
        self.setData({
          reportCountVisible: true
        })
      } else if (res.data.report != null) { //拍照回位置
        self.setData({
          reportBackVisible: true,
        })
      }
    }).catch(err => {
      console.log(err)
    });
  },

  reportBackClick() {
    let self = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: `${api.ReportBack}`,
          filePath: tempFilePaths[0],
          name: 'file',
          header: {
            'userToken': wx.getStorageSync('userToken')
          },
          success(res) {
            debugger
            let data = JSON.parse(res.data)
            if (data.code == 1) {
              $Toast({
                content: data.msg,
                type: 'success'
              });
            } else {
              $Toast({
                content: data.msg,
                type: 'error'
              });
            }
            self.setData({
              reportBackVisible: false
            })
          }
        })
      }
    })
  },
  goLogin() {
    wx.switchTab({
      url: '../../pages/my/my',
    })
  },
  clickChooseTime(val) {
    this.setData({
      currentTime: val.currentTarget.dataset.time
    })
  },
  openAuth() {
    let self = this
    wx.requestSubscribeMessage({
      tmplIds: ['ggVUb3Cb76E77EVf4eGxCc-JYnwTP8ilkqLCuh2I7JE'],
      success(res) {
        self.setData({
          authVisible: false
        })
      },
      fail(err) {
        debugger
      }
    });
  },
  clickBuild: function (item) {
    let self = this;
    console.log(item)
    wx.navigateTo({
      url: '../roomTime/room',
      events: {
        // 为指定事件添加一个监听器，获取被打开页面传送到当前页面的数据
        acceptDataFromOpenedPage: function (data) {
          console.log(data)
        },
        someEvent: function (data) {
          console.log(data)
        }
      },
      success: function (res) {
        // 通过eventChannel向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', {
          data: item.currentTarget.dataset.item
          , currentTime: self.data.currentTime
        })
      }
    })

  }

})
